package com.order.interceptor.business;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jfinal.aop.Interceptor;
import com.jfinal.core.ActionInvocation;
import com.jfinal.core.Controller;
import com.jfinal.kit.StringKit;
import com.order.config.Consts;
import com.order.model.Account;


/** 
* @ClassName: AccountLoginInterceptor 
* @Description: 客户拦截器
* @author LinBo
* @date 2014年4月25日 下午6:05:55 
*  
*/ 
public class AccountInterceptor implements Interceptor {

	Logger log = LoggerFactory.getLogger(AccountInterceptor.class);
	private Account account = new Account();
	
	@Override
	public void intercept(ActionInvocation ai) {
		Controller controller = ai.getController();
		System.out.println(controller.getRequest().getServerName());
		account = controller.getSessionAttr(Consts.ACCOUNT);
		log.info("当前请求客户：" + account);
		if(account == null || StringKit.isBlank(account.getStr(Account.NAME))) {
			//记录本次请求路径
			String requestURL = controller.getRequest().getServletPath();
			log.info("记录本次请求url:" + requestURL);
			controller.setSessionAttr(Consts.LAST_URL, requestURL);
			//注册页面
			log.info("客户没有登录或者session过期，重定向登录页面");
			controller.redirect("/toLogin");
		} else {
			ai.invoke();
		}
	}

}
